using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using LetLord.Models;
using LetLord.ViewModels;

namespace LetLord.Controllers
{
    public class ResidentialPropertiesController : Controller
    {
		private readonly IAddressRepository addressRepository;
		private readonly IResidentialPropertyRepository residentialpropertyRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public ResidentialPropertiesController() : this(new AddressRepository(), new ResidentialPropertyRepository())
        {
        }

        public ResidentialPropertiesController(IAddressRepository addressRepository, IResidentialPropertyRepository residentialpropertyRepository)
        {
			this.addressRepository = addressRepository;
			this.residentialpropertyRepository = residentialpropertyRepository;
        }

        // GET: /ResidentialProperties/ViewProperty
        [HttpGet]
        public ActionResult ViewProperty(int id)
        {
            try
            {
                return PartialView("_ViewPropertyPartial", residentialpropertyRepository.Find(id));
            }
            catch (Exception e)
            {
                e.InnerException.ToString();
            }
            return View("Error");
        }

        // GET: /ResidentialProperties/EditProperty
        [HttpGet]
        public ActionResult EditProperty(int id)
        {
            try
            {
                var viewModel = new EditPropertyViewModel();
                viewModel.Id = id;
                return PartialView("_EditPropertyPartial", viewModel);
            }
            catch (Exception e)
            {
                e.InnerException.ToString();
            }
            return View("Error");
        }

        // POST: /ResidentialProperties/EditProperty
        [HttpPost]
        public ActionResult EditProperty(EditPropertyViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var toUpdate = residentialpropertyRepository.Find(viewModel.Id);
                    toUpdate.Address.Line1 = viewModel.Line1;
                    toUpdate.Address.Line2 = viewModel.Line2;
                    toUpdate.Address.TownCity = viewModel.TownCity;
                    toUpdate.Address.County = viewModel.County;
                    toUpdate.Address.PostCode = viewModel.PostCode;
                    toUpdate.PropertyTypeString = viewModel.PropertyTypeString;
                    toUpdate.SizeSquareMeters = viewModel.SizeSquareMeters;
                    toUpdate.NumberOfBedrooms = viewModel.NumberOfBedrooms;
                    toUpdate.NumberOfBathrooms = viewModel.NumberOfBathrooms;
                    toUpdate.NumberOfReceptionRooms = viewModel.NumberOfReceptionRooms;
                    toUpdate.MonthlyRate = viewModel.MonthlyRate;
                    toUpdate.Description = viewModel.Description;
                    toUpdate.HasBackGarden = viewModel.HasBackGarden;
                    toUpdate.HasFrontGarden = viewModel.HasFrontGarden;
                    toUpdate.HasSecureParking = toUpdate.HasSecureParking;
                    toUpdate.IsDisabledFriendly = toUpdate.IsDisabledFriendly;
                    residentialpropertyRepository.InsertOrUpdate(toUpdate);
                    residentialpropertyRepository.Save();

                    return Json(new { error = false, message = "Property edited" });
                }
                catch (Exception e)
                {
                    e.InnerException.ToString();
                }
            }
            return View("Error");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                addressRepository.Dispose();
                residentialpropertyRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

